package org.vectrics.common.db;



import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.vectrics.DataException;


/**
 * @author mmoore
 */

public class JdbcExecuteBase {
	private transient static Logger log = Logger.getLogger(JdbcExecuteBase.class);
	private Session session = null;
	
	public JdbcExecuteBase(Session session) {
		super();
		this.session = session;
	}
	
	public Session getSession() {
		return(session);
	}

	/*	
	protected static DataSource getDataSource(String jndiName) {
		DataSource dataSource = null;
		try {
			InitialContext ic = new InitialContext();
			dataSource = (DataSource)ic.lookup(jndiName);
			if (dataSource == null) {
				log.error("jndi not found for dataSource = " + jndiName);
			}
		} catch (NamingException ne) {
			log.error("Exception getting datasource: " + jndiName, ne);
		}
		return(dataSource);
	}
	*/
	
	protected Connection getDBConnection() {
		Connection dbConnection = null;
		try {
			Session hibernateSession = this.getSession();
			dbConnection = hibernateSession.connection();
		}
		catch (Exception ex) {
			throw new DataException("Exception getting connection from hibernate", ex);
		}
		return(dbConnection);
	}







	//------------------------------------------------------------------------------------
	protected void closeStatement(Statement stmt) {
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (SQLException se) {
			log.error("closeStatement() - exception", se);
		}
	}
	
	
}
